<?php
//PingBuWang.Com
//Copyright (c) 2017~2099 http://www.pingbuwang.com All rights reserved.
namespace app\tag\controller;

use mip\AdminApiBase;
class ApiAdminTag extends AdminApiBase
{
     protected $beforeActionList = ['start'];
    public function start() {
    }
    public function index(){

    }
    public function tagAdd()
    {
        $title = input('post.title');
      	if(!$title){
      	  return jsonError('请输入标签名');
      	}
        $tagInfo = db('tags')->where('title',$title)->find();
      	if($tagInfo){
      		return jsonError('标签已存在');
      	}

      	$arrTags =array(
            'id' => uuid(),
            'title' => $title,
        );

      	if( model('app\tag\model\TagsArticle')->updateByTags($arrTags) ) {
            db('tags')->insert($arrTags);
        }

        return jsonSuccess('添加成功');
        

    }
    
    public function itemDel()
    {
        $id = input('post.id');
        if (!$id) {
          return jsonError('缺少参数');
        }
        $itemInfo = db('tags')->where('id',$id)->find();
        if (!$itemInfo) {
          return jsonError('删除项不存在');
        }
        db('tags')->where('id',$id)->delete();
        db('tagsArticle')->where('tags_id',$id)->delete();
        return jsonSuccess('成功');
    }
    
    public function TagsDel(){
        $tagIds = input('post.tagIds');
        if (!$tagIds) {
      	  return jsonError('缺少参数');
      	}
        $tagIds = explode(',',$tagIds);
      	if (is_array($tagIds)) {
            foreach ($tagIds as $id){
                $tagInfo = db('tags')->where('id',$id)->find();
                if ($tagInfo) {
                   db('tags')->where('id',$id)->delete();
                }
            }
            return jsonSuccess('删除成功');
        } else {
        	   return jsonError('参数错误');
        }

    }

    public function tagsEdit()
    {
		$id = input('post.id');
		$title = input('post.title');

		if(!$id){
			return jsonError('缺少ID');
		}
		if(!$TagsInfo = db('tags')->where('id',$id)->find()){
          	return jsonError('标签不存在');
        }
        $itemInfo = db('tags')->where('id','<>',$id)->where('title',$title)->find();
        if ($itemInfo) {
          return jsonError('名称已存在，请重新输入');
        }
        if(db('tags')->where('id',$id)->update(array('title' => $title))){
            model('app\tag\model\TagsArticle')->updateByTags(array('id'=>$id,'title' => $title) );
            return  jsonSuccess('修改成功');
        }else{
            return  jsonError('修改失败,或者是标签名未改变');
        }


    }

    public function tagList()
    {
        $page = input('post.page');
        $limit = input('post.limit');
        $keywords = input('post.keywords');

        if (!$page) {
            $page = 0;
        }
        if (!$limit) {
            $limit = 10;
        }

        $where='';
        if(!empty( $keywords ) ){
            $where = 'title like `%'.$keywords.'%` ';
        }

        $itemList = model('app\tag\model\Tags')->getItemList($where,'create_time desc',$page,$limit);
        $itemCount = model('app\tag\model\Tags')->getCount($where);
        return jsonSuccess('',['tagsList' => $itemList,'total' => $itemCount,'page' => $page]);
    }

}